package com.controller;

import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/org/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/register")
    public String hello(@RequestBody User user) {
        return userService.registerUser(user) ? "操作成功!" : "操作失败";
    }
    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping
    public String updateUser(@RequestBody User user){
        return userService.updateUser(user) ? "操作成功!" : "操作失败";
    }

    /**
     * 列出所有的用户
     * @return
     */
    @GetMapping
    public List<User> listUser(){
        return userService.listUser();
    }


    /**
     * 根据用户名删除用户
     * @param username
     * @return
     */
    @DeleteMapping("/{username")
    public String deleteUserByUsername(@PathVariable String username){
        return userService.deleteUserByUsername(username) ? "操作成功!" : "操作失败";
    }

    /**
     *  根据用户名查询用户
     * @param username
     * @return
     */
    @GetMapping("/{username}")
    public User selectByUserName(@PathVariable String username){
        return userService.selectByUserName(username);
    }
}
